<template>
	<div class="index">

		<div class="center">
			<left></left>
			<div class="right">
          <top></top>
          <div class="hei10"></div>
          <div class="where" style="background: #fff; padding: 10px 30px; font-size: 14px; color: #595959;">当前位置：<router-link to="/index" style="color: #409eff;">分销后台</router-link>><router-link style="color: #409eff;" to="/order_production">生产发货</router-link>>导出生产</div>
          <div class="hei10"></div>

				<div class="title">导出生产</div>
				<div class="my">
					<div class="myid">本次导出：{{everyday}}——现在</div>
					<div>
						<el-button type="danger" round @click="daochu" :loading="daochuing">{{daochuingz}}
						</el-button>
						<el-button type="danger" round @click="log = true">按条件导出生产</el-button>

					</div>
				</div>
				<div class="exportall" v-loading="loading">
					<div class="list">
						<div>数据生成时间</div>
						<div>下载数据</div>
					</div>
					<div class="list l2" v-for="item in list" :key="item.id">
						<div>{{item.created_at}}</div>
						<div v-if="item.status != -1">
							<el-button type="text" :loading="item.status != 2"  @click="xiazai(item.id)">
								下载数据</el-button>
						</div>
						<div v-if="item.status == -1">
							<el-button type="text"  @click="xiazai(item.id)">本次下载缺少数据，重新点击“导出待生产”，下载本次数据
							</el-button>
						</div>

					</div>
				</div>
			</div>
		</div>

		<el-dialog center title="输入条件导出生产" :visible.sync="log" width="600px" :close-on-click-modal="false">
			<div style="padding-right: 30px;">
				<el-form ref="form" @submit.native.prevent :model="form" label-width="150px">
					<div style="width: 138px; text-align: right;">
						 <el-radio v-model="radio" label="1">通过订单来源导出:</el-radio></div>
					<div style="display: flex; align-items: center; padding: 10px 0 0 30px;" >
						<el-input style="width: 150px; margin-right: 10px;" v-model="pro"  placeholder="输入订单来源"></el-input>
						<el-button style=" margin-right: 10px;" type="primary" size="medium" @click='searchproname'>搜索</el-button>
						<div v-if="fixpronames">产品：{{fixpronames}}</div>
					</div>

					<div style="width: 138px; text-align: right; margin-top: 50px;">
						 <el-radio v-model="radio" label="2">通过产品编码导出:</el-radio></div>
					<div style="display: flex; align-items: center; padding: 10px 0 0 30px;" v-for="(item,index) in codeall" :key="index" >
						<el-input style="width: 150px; margin-right: 10px;" v-model="item.code"  placeholder="输入产品编码"></el-input>

						<div v-if="item.result">产品：{{item.result}}</div>
					</div>
					<div style="padding-left: 30px; padding-top: 20px;">
						<el-button type="primary" size="medium" @click='addcodeall'>加一条</el-button>
						<el-button style=" margin-right: 10px;" type="primary" size="medium" @click='searchprocode()'>检查产品编码</el-button>
					</div>

					<!-- <el-form-item label="查询结果:">
						{{fixpronames}}
					</el-form-item> -->
				</el-form>

			</div>
			<div slot="footer" class="dialog-footer">
				<el-button type="info" round @click="log = false">取 消</el-button>
				<el-button type="danger" round @click="daochu2()" :loading="daochuing">确 定</el-button>
			</div>
		</el-dialog>


		<el-dialog center title="请输入导出密码" :visible.sync="mimalog" width="400px" :close-on-click-modal="false">
			<div style="padding-right: 30px;">
				<el-input v-model="mima" show-password placeholder="请输入导出密码"></el-input>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button type="danger" round v-if="this.daoorxia != 'dao22'" @click="mimaqueren">确 定
				</el-button>
				<el-button type="danger" round :loading="daochuing" v-if="this.daoorxia == 'dao22'" @click="mimaqueren22">确 定
				</el-button>
				<el-button type="danger" round  @click="mimachongzhilog = true">重设密码</el-button>
			</div>
		</el-dialog>

		<el-dialog center title="请设置导出密码" :visible.sync="mimasetlog" width="400px" :close-on-click-modal="false">
			<!--              <div style="padding-right: 30px; margin-bottom: 20px;">
                   <el-input v-model="mimashoujihao" disabled placeholder="请输入验证码"></el-input>
              </div>
              <div style="padding-right: 30px; margin-bottom: 20px; position: relative;">
                   <el-input v-model="mimacode"  placeholder="请输入验证码"></el-input>
                   <el-button style="padding: 9px 30px; position: absolute; right: 31px; top: 1px;"  type="danger" @click="getcode" :disabled="jinyong" size="mini">{{tishi}}</el-button>
              </div> -->
			<div style="padding-right: 30px; margin-bottom: 20px;">
				<el-input v-model="mimaset" show-password placeholder="请设置导出密码"></el-input>
			</div>
			<div style="padding-right: 30px;">
				<el-input v-model="mimaset2" show-password placeholder="请再次输入确认导出密码"></el-input>
			</div>
			<div style="color: #DF3434; margin-top: 10px;">为了您的订单数据安全，即日起所有批量导出数据的出口需输入导出密码才可导出，请与联系店铺负责人协商设置该密码</div>
			<div slot="footer" class="dialog-footer">
				<el-button type="info" round @click="mimasetlog = false">取 消</el-button>
				<el-button type="danger" round @click="mimasetqueren">确 定</el-button>
			</div>
		</el-dialog>

		<el-dialog center title="重设密码" :visible.sync="mimachongzhilog" width="400px" :close-on-click-modal="false">
			<div style="padding-right: 30px; margin-bottom: 20px;">
				<el-input v-model="mimashoujihao" disabled placeholder="请输入验证码"></el-input>
			</div>
			<div style="padding-right: 30px; margin-bottom: 20px; position: relative;">
				<el-input v-model="mimacode" placeholder="请输入验证码"></el-input>
				<el-button style="padding: 9px 30px; position: absolute; right: 31px; top: 1px;" type="danger"
					@click="getcode" :disabled="jinyong" size="mini">{{tishi}}</el-button>
			</div>
			<div style="padding-right: 30px; margin-bottom: 20px;">
				<el-input v-model="mimaset" show-password placeholder="请输入新密码"></el-input>
			</div>
			<div style="padding-right: 30px;">
				<el-input v-model="mimaset2" show-password placeholder="请确认新密码"></el-input>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button type="info" round @click="mimachongzhilog = false">取 消</el-button>
				<el-button type="danger" round @click="mimaqueren2">确 定</el-button>
			</div>
		</el-dialog>


		<!-- <foot></foot> -->
	</div>
</template>

<script>
	import axios from "axios";
	import top from '@/components/top.vue'
	import left from '@/components/left.vue'
	import foot from '@/components/foot.vue'
	export default {
		components: {
			"left": left,
			"top": top,
			"foot": foot
		},
		name: 'kanban',
		data() {
			return {
				radio:'1',
				msg: "hello",
				options: "",
				value: "",
				list: [],
				loading: true,
				url: "",
				everyday: '',
				prolist: "",
				pro: "",
				pros: 0,
				codeall: [
					{code:"",result:"",id:""}
				],
				log: false,
				daochuing: false,
				daochuingz: "导出全部待生产",
				daochuingz2: "导出数据",
				lunxun: "",
				fixpronames: "",
				fixprocodeshow: false,
				fixpronameshow: false,
				form: {},
				fixproid: "",
				mimalog: false,
				mima: "",
				mimaid: "",
				mimasetlog: false,
				mimaset: "",
				mimaset2: "",
				mimachongzhilog: false,
				mimashoujihao: "",
				mimacode: "",
				count: 60,
				tishi: "获取验证码",
				jinyong: false,
				daoorxia: 'dao',
				dao: ""
			}
		},
		created() {
			this.getpro()
			this.onelist();
			this.getshoujihao()
		},

		destroyed() {
			// clearTimeout(this.lunxun)
		},
		methods: {


			addcodeall:function(){
				if(this.codeall.length <10){
					this.codeall.push({code:"",result:"",id:""})
				}else{
					this.$message.error('每次最多10条');
				}
			},

			//轮询获取下载是否完成
			onelist: function() {
				this.getlist()
				this.loading = true
				// var that = this
				// that.lunxun = setTimeout(function(){that.onelist()},10000)
			},
			//获取所有产品
			getpro: function() {
				var that = this

				axios.get("/api/gth/products?_page_size=10000")
					.then(function(response) {
						that.prolist = response.data.data.data
					})
			},
			getlist: function() {
				axios.get("/api/plan-market/export/logs?order_by=0")
					.then(response => {
						this.list = response.data.data.data
						this.loading = false
						this.everyday = this.list[0].created_at
					})
			},
			//重置密码确认
			mimaqueren2: function() {
				if (this.mimaset != this.mimaset2) {
					this.$message.error("输入的密码不一致");
					return false
				}
				axios.post("/api/plan-market/shop/pwd", {
						mobile: this.mimashoujihao,
						sms_code: this.mimacode,
						password: this.mimaset
					})
					.then(response => {
						if (response.data.msg.code == 0) {
							this.$message.success("密码设置成功");
							this.mimachongzhilog = false
						} else {
							this.$message.error(response.data.msg.info);
						}
					})
			},
			//获取验证码
			getcode: function() {
				var that = this;
				var params = new URLSearchParams();
				params.append('mobile', this.mimashoujihao);
				axios.post('/api/auth/sms_code', params)
					.then(response => {
						if (response.data.msg.code == 0) {
							this.$message({
								message: '验证码已发送！',
								type: 'success'
							});
							that.daojishi()
						} else {
							this.$message.error(response.data.msg.info);
						}
					})
			},
			//倒计时功能
			daojishi: function() {
				var interval;
				interval = window.setInterval(() => {
					if (this.count <= 0) {
						window.clearInterval(interval)
						this.tishi = "获取验证码",
							this.jinyong = false
						this.count = 60
					} else {
						this.count -= 1;
						this.tishi = this.count + "秒重新获取"
						this.jinyong = true
					}
				}, 1000);
			},
			//获取手机号发验证码
			getshoujihao: function() {
				axios.get("/api/gth/my/shop/mobile")
					.then(response => {
						if (response.data.msg.code == 0) {
							this.mimashoujihao = response.data.data
						} else {
							this.$message.error(response.data.msg.info);
						}
					})
			},
			//密码设置
			mimasetqueren: function() {
				if (this.mimaset != this.mimaset2) {
					this.$message.error("输入的密码不一致");
					return false
				}

				axios.post("/api/plan-market/shop/pwd", {
						mobile: this.mimashoujihao,
						sms_code: this.mimacode,
						password: this.mimaset
					})
					.then(response => {
						if (response.data.msg.code == 0) {
							this.$message.success("密码设置成功");
							this.mimasetlog = false
						} else {
							this.$message.error(response.data.msg.info);
						}
					})
			},
			//确认密码下载
			mimaqueren: function() {
				var that = this
				if (this.daoorxia == "dao") {
					this.daochuing = true
					this.daochuingz = "生成数据中..."
					axios.post("/api/plan-market/order/export", {
							type: 2,
							password: this.mima
						})
						.then(function(response) {
							if (response.data.msg.code == 0) {
								that.loading = true
								that.getlist();
								that.daochuingz = "导出全部待生产"
								that.daochuing = false
								that.mimalog = false
							} else {
								that.$message.error(response.data.msg.info);
								that.daochuing = false
								that.daochuingz = "导出全部待生产"
							}
						});
				} else {
					axios.post("/api/plan-market/shop/log/url", {
							id: this.mimaid,
							password: this.mima
						})
						.then(response => {
							if (response.data.msg.code == 0) {
								// if (window.location.href.indexOf("91haoka_platform") > -1) {
								// 	console.log("线上项目，谨慎操作。")
								// 	this.url = window.location.origin + "/"
								// } else {
								// 	this.url = ""
								// }
								that.mimalog = false
								var a = document.createElement('a');

								a.setAttribute('href', window.location.origin + "/" + response.data.data.local_filename);
								a.click();
							} else {
								this.$message.error(response.data.msg.info);
							}
						})
				}

			},
			searchprocode:async function() {
				for(let i in this.codeall){
					if(this.codeall[i].code != ''){
						await axios.get('/api/plan-market/product/get?sku='+this.codeall[i].code)
							.then(response => {
								if (response.data.msg.code != 0) {
									this.$message.error(response.data.msg.info);
									return false
								} else {
									if (response.data.data == null) {
										this.$message.error('没有查询到此商品');
										return false
									} else {
										this.codeall[i].result = response.data.data.name
										this.codeall[i].id = response.data.data.id
									}
								}
							})
					}

				}
			},
			//修改产品查询
			searchproname:async function() {
				await axios.get('/api/plan-market/product/get?name='+encodeURIComponent(this.pro))
					.then(response => {
				        if(response.data.msg.code != 0){
				        	this.$message.error(response.data.msg.info);
				        }else{
				            if(response.data.data == null){
				                this.fixpronames = "销售中心无该产品信息"
				                this.fixproid = ""
				            }else{
				                this.fixpronames = response.data.data.name
				                this.fixproid = response.data.data.id
				            }
				        }
				    })
			},
			xiazai:function(id) {
				this.mimaid = id
				this.daoorxia = 'xia'
				//判断有没有密码
				var that = this
				axios.get("/api/plan-market/shop/pwd")
					.then(function(response) {
						if (response.data.msg.code == 0) {
							console.log(response)
							if (response.data.data == "") {
								//要设置密码
								that.mimasetlog = true
							} else {
								//输入密码
								that.mimalog = true
							}
						} else {
							that.$message.error(response.data.msg.info);
						}
					});

				//没有密码设置  有密码下载
				//地址配置,用于下载模板
				// if(window.location.href.indexOf("91haoka_platform") > -1){
				// 	console.log("线上项目，谨慎操作。")
				// 	this.url = window.location.origin+"/"
				// }else{
				// 	this.url = ""
				// }
				// var a = document.createElement('a');

				// a.setAttribute('href', this.url+url);
				// a.click();
			},
			daochu: function() {
				this.daoorxia = 'dao'
				var that = this
				axios.get("/api/plan-market/shop/pwd")
					.then(function(response) {
						if (response.data.msg.code == 0) {
							console.log(response)
							if (response.data.data == "") {
								//要设置密码
								that.mimasetlog = true
							} else {
								//输入密码
								that.mimalog = true
							}
						} else {
							that.$message.error(response.data.msg.info);
						}
					});

				// this.daochuing = true
				// this.daochuingz = "生成数据中..."
				// var that = this
				// axios.get("/api/plan-market/order/export?type=2")
				//   .then(function(response) {
				//       if(response.data.msg.code == 0){
				//             that.loading = true
				//             that.getlist();
				//             that.daochuingz = "导出全部待生产"
				//             that.daochuing = false
				//       }else{
				//             that.$message.error(response.data.msg.info);
				//             that.daochuing = false
				//             that.daochuingz = "导出全部待生产"
				//       }
				//   });
			},
			mimaqueren22:async function() {
				this.daochuing = true
				let obj
				if(this.radio == 1){
					await this.searchproname()
					obj={
						type: 2,
						sp_id: this.fixproid,
						source: this.pro,
						password: this.mima
					}
				}else{
					await this.searchprocode()
					let arr = []
					for(let i in this.codeall){
						if( this.codeall[i].id == ''){

						}else{
							arr.push(this.codeall[i].id)
						}

					}
					arr = arr.toString()

					obj={
						type: 2,
						sp_id: arr,
						password: this.mima
					}
				}
				// if (this.pro == "" && this.codeall[0].id == "") {
				// 	this.$message.error('至少选择一项！');
				// 	return false
				// }
				// if (this.fixproid == "" && this.pros == 0) {
				// 	this.$message.error('请先选择产品在导出！');
				// 	return false
				// }
				this.daochuing = true
				this.daochuingz2 = "生成数据中..."
				var that = this

				axios.post("/api/plan-market/order/export", obj)
					.then(function(response) {
						if (response.data.msg.code == 0) {
							that.loading = true
							that.log = false
							that.mimalog = false
							that.getlist();
							that.daochuingz2 = "导出数据"
							that.daochuing = false
						} else {
							that.$message.error(response.data.msg.info);
							that.daochuingz2 = "导出数据"
							that.daochuing = false
						}
					});
			},
			daochu2: function() {

				this.daoorxia = 'dao22'
				var that = this
				axios.get("/api/plan-market/shop/pwd")
					.then(function(response) {
						if (response.data.msg.code == 0) {
							console.log(response)
							if (response.data.data == "") {
								//要设置密码
								that.mimasetlog = true
							} else {
								//输入密码
								that.mimalog = true
							}
						} else {
							that.$message.error(response.data.msg.info);
						}
					});



			}
		}
	}
</script>

<style lang="scss" scoped>
	.index {
        /deep/ .el-button--danger {
            color: #FFF;
            background-color: #2974FF;
            border-color: #2974FF;
        }

				.title {
					border-bottom: 1px #EAEAEA solid;
					line-height: 45px;
					padding-left: 30px;
					font-size: 16px;
					background: #F9F9F9;
					color: #727272;
				}

				.my {
					display: flex;

					margin: 30px auto;

					.myid {
						font-size: 14px;
						color: #DD2727;
						padding-left: 10px;
						line-height: 30px;
						margin-right: 20px;
					}

				}

				.exportall {

					margin: 0 auto;
					border: 1px #EAEAEA solid;
					border-right: 0;
					border-bottom: 0;

					.list {
						display: flex;
						text-align: center;
						border-bottom: 1px #EAEAEA solid;
						font-size: 14px;

						div {
							flex: 1;
							line-height: 45px;
							border-right: 1px #EAEAEA solid;
							color: #666;
						}
					}

					.l2 {
						font-size: 14px;
						color: #727272
					}
				}

	}
</style>
